<template>
	<view class="content">
		<view class="list" v-for="(item, index) in addressList" :key="index">
			<view>
				<text>收货人:{{item.name||""}}</text>
				<text class="r">{{item.phone}}</text>
			</view>
			<view>
				<text>收货地址:{{item.area||""}}{{item.address||""}}</text>
			</view>
			<view class="foot">
				<uni-icons @tap="defAddress(item.addressId)" type="circle" size="26" color="#999" v-if="item.def!=1"></uni-icons>
				<uni-icons @tap="defAddress(item.addressId)" type="checkbox-filled" size="26" color="#ee3535" v-else></uni-icons>
				默认地址
				<view class="r">
					<text class="yticon icon-bianji" @click.stop="addAddress('edit', item)">编辑</text>
					<image :src="imgBase+'/static/image/user/my_delete@2x.png'" mode="widthFix" class="del"></image>
					<text class="yticon" @tap="delAddress(item.def,item.addressId, index)">删除</text>
				</view>
			</view>
		</view>
		<button class="add-btn" @click="addAddress('add')" v-if="addressList.length<5">新增地址</button>
	</view>
</template>

<script>
	import uniIcons from "@/pages/aduit/components/uni-icons/uni-icons.vue"
	import ajax from '@/common/request.js';
	import api from '@/common/api.js';
	var that="";
	export default {
		components: {
			uniIcons
		},
		data() {
			return {
				source: 0,
				mid:"",
				orderId:"",//订单修改地址
				type:"",//兑换商城订单修改地址
				addressList: [],
				imgBase:""
			}
		},
		onLoad(e){
			that=this;
			that.imgBase=api.getImgBaseSrc();
			that.mid='61359';
			if(e.id){
				that.orderId=e.id;
			}
			if(e.type){
				that.type=e.type
			}
		},
		onShow(){
			if(that.mid){
				that.loadData()
			}
		},
		methods: {
			loadData(){
				ajax({
					url:"/user/address/searchAddress",
					method:"GET",
					data:{
						mid:that.mid
					},success(d){
						that.addressList=d;
					}
				})
			},
			
			addAddress(type, item){
				uni.navigateTo({
					url: `/pages/address/addressManage?type=${type}&data=${JSON.stringify(item)}&length=${that.addressList.length}`
				})
			},
			//修改默认地址
			defAddress(id){
				if(that.orderId){
					if(that.type==1){
						ajax({
							url:"/timeBeanOrder/updateTimeOrderAddress",
							data:{
								timeOrderId:that.orderId,
								addressId:id
							},success(d){
								api.msg("修改成功,2s后返回");
								setTimeout(function(){
									uni.navigateBack({
										delta:1
									})
								},2000)
							}
						})
					}else{
						//修改订单地址
						ajax({
							url:"/order/updateOrderAddress",
							data:{
								orderId:that.orderId,
								addressId:id
							},success(d){
								api.msg("修改成功,2s后返回");
								setTimeout(function(){
									uni.navigateBack({
										delta:1
									})
								},2000)
							}
						})
					}
					
				}else{
					ajax({
						url:"/user/address/updateAddress",
						data:{
							def:1,
							mid:that.mid,
							addressId:id
						},success(d){
							that.loadData()
						}
					})
				}
			},
			//删除地址
			delAddress(def,id,index){
				if(def==1){
					api.msg("默认地址不能进行删除!");
					return;
				}
				uni.showModal({
					title:"温馨提示",
					content:"您是否要确认删除这条地址?",
					success: function (res) {
						if (res.confirm) {
							ajax({
								url:"/user/address/delAddress",
								data:{
									mid:that.mid,
									addressId:id
								},success(d){
									that.addressList.splice(index,1)
								}
							})
						} else if (res.cancel) {
						}
					}
				})
			}
		}
	}
</script>

<style lang='scss'>
	page{
		padding-bottom: 120rpx;
		background: #f2f2f2;
	}
	.uni-icon{
		margin-top: -10rpx;
		display: inline-block !important; 
		margin-right: 10rpx;
	}
	.icon-bianji:before{
		margin-right: 10rpx;
	}
	.list{
		padding: 30rpx;
		padding-bottom: 0;
		margin-top: 20rpx;
		font-size: 28rpx;
		background: #fff;
		.r{
			float: right;
		}
		.del{
			width:28rpx;
			height:28rpx;
			margin-right: 10rpx;
			margin-left: 10rpx;
		}
		>view{
			margin-bottom: 30rpx;
		}
		.foot{
			line-height: 80rpx;
			border-top:1px solid #eee;
		}
	}
	.add-btn{
		width:525rpx;
		text-align: center;
		height:72rpx;
		line-height: 72rpx;
		color:#fff;
		font-size: 28rpx;
		background:linear-gradient(90deg,rgba(252,165,52,1),rgba(238,53,53,1));
		border-radius:20rpx;
	}
</style>
